package com.repast.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.repast.common.ServerResponse;
import com.repast.pojo.User;
import com.repast.service.IUserService;
import com.repast.utils.GetOpenId;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private IUserService userService;

	//添加用户信息,数据库存在就添加,不存在就添加
	@RequestMapping("addUser.do")
	@ResponseBody
	public ServerResponse addUser(HttpServletRequest request,HttpServletResponse response,
			String jscode,String userInfo) throws Exception{
		String openid = GetOpenId.getOpenid(jscode);
		//String openid = "oSNL50I0AM4CCK37pelOGbHliy-E";
		User user = new User();
		JSONObject jsonUser = JSONObject.fromObject(userInfo);
		String nickName = jsonUser.getString("nickName");
		user.setUserNickname(nickName);
		user.setUserAvatorUrl(jsonUser.getString("avatarUrl"));
		user.setUserGender(Integer.parseInt(jsonUser.getString("gender")));//0女;1男
		//user.setUserCreateTime(userCreateTime);
		String location =new StringBuilder().append(jsonUser.getString("province")).append(jsonUser.getString("city")).toString();
		user.setUserLocation(location);
		return userService.addUser(user,openid);
	}
}
